package com.test.A20;

import java.util.Arrays;

import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Configuration
@ComponentScan
@PropertySource("classpath:application.properties")
@EnableConfigurationProperties({WebMvcProperties.class, ServerProperties.class})
//读取配置文件的注解 WebMvcProperties( spring.mvc开头)   ServerProperties(server开头)
public class WebConfig {
  
	 /****
	  * 初始化tomcat容器   ⬅️内嵌 web 容器工厂
	  * @return
	  */
	 @Bean
	    public TomcatServletWebServerFactory tomcatServletWebServerFactory(ServerProperties serverProperties) {
	        return new TomcatServletWebServerFactory(serverProperties.getPort());//可以读取配置文件的 端口  serverProperties.getPort()
	    }
	 
	 /***
	  *  ⬅️创建 DispatcherServlet
	  */
	    @Bean
	    public DispatcherServlet dispatcherServlet() {
	        return new DispatcherServlet();
	    }

	    /*****
	     * ⬅️注册 DispatcherServlet, Spring MVC 的入口
	     */
	    @Bean
	    public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(DispatcherServlet dispatcherServlet,WebMvcProperties webMvcProperties) {
	        DispatcherServletRegistrationBean registrationBean = new DispatcherServletRegistrationBean(dispatcherServlet, "/");
	        registrationBean.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
	        //webMvcProperties.getServlet().getLoadOnStartup() 读取配置文件的spring.mvc.servlet.load-on-startup
	        return registrationBean;
	    }
	    
	 // 如果用 DispatcherServlet 初始化时默认添加的组件, 并不会作为 bean, 给测试带来困扰
	    // ⬅️1. 加入RequestMappingHandlerMapping
	    @Bean
	    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
	        return new RequestMappingHandlerMapping();
	    }
	    
	    
	    /****
	     * 继续加入RequestMappingHandlerAdapter
	     * @return
	     */
	    @Bean
	    public MyRequestMappingHandlerAdapter requestMappingHandlerAdapter() {
	    	TokenArgumentResolver tokenArgumentResolver = new TokenArgumentResolver();
	    	 YmlReturnValueHandler ymlReturnValueHandler = new YmlReturnValueHandler();
	        MyRequestMappingHandlerAdapter handlerAdapter = new MyRequestMappingHandlerAdapter();
	        handlerAdapter.setCustomArgumentResolvers(Arrays.asList(tokenArgumentResolver));//把自定义的请求参数解析器放进去
	        handlerAdapter.setCustomReturnValueHandlers(Arrays.asList(ymlReturnValueHandler));//把自定义的返回值解析器放进去
	        return handlerAdapter;
	    }
	 
}
